home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13045 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Can anyone HELP with variable delaration? Please?
  5. Date: 3 Apr 1996 18:29:40 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4jvc6kINNk1j@keats.ugrad.cs.ubc.ca>
  8. References: <4jt07b$dul@news.us.net> <3162119a.43634755@news.ucs.ubc.ca> <4juf2kINNe9v@keats.ugrad.cs.ubc.ca> <3162de71.18703379@news.ucs.ubc.ca>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <3162de71.18703379@news.ucs.ubc.ca>,
  12. James Fairweather <jamesdf@unixg.ubc.ca> wrote:
  13. >>Any variable can be declared constant, including strings:
  14. >>
  15. >>    const char * const str = "Foo";
  16. >>
  17. >>Or do you mean literal constants?
  18. >>
  19. >I'm not sure what the difference is.  I thought the keyword "const"
  20. >was just for the compiler.  If you tried to modify the variable
  21. >declared const, the compiler would complain.  Does it do something
  22. >else with the variable if you declare it const?
  23.  
  24. It's a language feature which designates an unmodifiable lvalue, as you say.
  25. Though around here we are careful to distinguish the C language itself from C
  26. language compilers. If you don't make that distinction, then a _lot_ of things
  27. easily become ``for the compiler only''. Structs are surely for the compiler
  28. only---after all, there are no data structures at the machine code level, just
  29. addressing!
  30.  
  31. >I guess I was being too extreme.  It's just that in high school, when
  32. >we were coding with Pascal, we were encouraged to use strings
  33. >whereever possible.  Don't ask me why; it never occurred to me to ask
  34. >at the time.  And sometimes when I read other people's code,
  35. >espeically people who are used to Pascal, I often see them using
  36. >strings in an if...else situation.  In these cases, a well-name
  37. >#defined constant works better, because you can use switch and the
  38. >comparison is faster.
  39.  
  40. Ah, I thought that's what you might have been referring to. This sort of abuse
  41. isn't something you would see too much of in C. In Pascal, which has string
  42. operators, it is more natural.
  43.  
  44. A Pascal compiler can optimize some string comparisons into fast inline code.
  45. E,g, a comparison like IF string = "ABCD" might be turned into a straight
  46. 32-bit comparison on a suitable architecture.
  47.  
  48. Nevertheless, whoever taught you that in highschool was plainly nuts. But then
  49. again, kooky ``computer science'' teachers are plentiful at the higschool
  50. level.
  51. -- 
  52.  
  53.